package com.msb.controller;


import com.msb.pojo.Role;
import com.msb.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chshh
 * @since 2022-10-21
 */
@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @GetMapping("/query")
    public String query(Role user, Model model) throws Exception{
        List<Role> list = roleService.list();
        System.out.println(list);
        model.addAttribute("list",list);
        return "role";
    }

    /**
     * 处理页面跳转
     * @return
     */
    @RequestMapping("/roleDispatch")
    public String handlePageDispatch(Integer roleId,Model model, HttpSession session){
        if (roleId != null){
            session.setAttribute("roleId",roleId);
        }
        return "/updateRole";
    }

    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Role role,HttpSession session) throws Exception{
        if (session.getAttribute("roleId") != null){
            role.setRoleId((Integer)session.getAttribute("roleId"));
        }
        roleService.saveOrUpdate(role);
        return "redirect:query";
    }

    @RequestMapping("/delRole")
    public String delRole(Integer roleId){
        roleService.removeById(roleId);
        return "redirect:query";
    }

}

